﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using nwrole.Module.lib;

namespace nwrole.Module.lib
{
    public class ModuleEventLogger : IModuleLogger
    {
        private Object _logLock = new Object();

        private LogLevel _logLevel;

        private string _sourceName;

        public ModuleEventLogger(LogLevel logLevel = LogLevel.Error, string logName = "nwrole", string sourceName = "nwrole")
        {
            _logLevel = logLevel;
            _sourceName = sourceName;

            if (!EventLog.SourceExists(_sourceName))
                EventLog.CreateEventSource(_sourceName, logName);
        }

        public void Log(LogLevel level, string msg)
        {
            if (level <= _logLevel)
            {
                try
                {
                    EventLog.WriteEntry(_sourceName, msg, GetEventLogEntryType(level));
                }
                catch(Exception e)
                {
                    int ehts = 3;
                }
            }
        }

        private static EventLogEntryType GetEventLogEntryType(LogLevel logLevel)
        {
            switch (logLevel)
            {
                case LogLevel.Info:
                case LogLevel.Debug:
                    return EventLogEntryType.Information;
                case LogLevel.Warning:
                    return EventLogEntryType.Warning;
                case LogLevel.Error:
                case LogLevel.Critical:
                    return EventLogEntryType.Error;
            }

            return EventLogEntryType.Error;
        }

    }
}
